package com.hkyp.ss.course.apis.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by L on 2017/2/27.
 * 正则匹配工具类
 */
public class MatcherUtil {


    /**
     * 正则匹配邮箱地址
     *
     * @param email
     * @return
     */
    public static boolean matcherEmail(String email) {
        //邮箱地址进行正则
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean isMatched = matcher.matches();
        return isMatched;
    }

    /**
     * 正则匹配手机号
     *
     * @param phone
     * @return
     */
    public static boolean matcherPhone(String phone) {
        //邮箱地址进行正则
        String check = "^(1[3,4,5,7,8])\\d{9}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(phone);
        boolean isMatched = matcher.matches();
        return isMatched;
    }

    /**
     * 生成6位数字验证码
     * @return
     */
    public static String getRandom() {
        String num = "";
        for (int i = 0 ; i < 6 ; i ++) {
            num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
        }
        return num;
    }

}
